# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=jolt-physics
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.0.1
pkgrel=1
pkgdesc="A multi core friendly rigid body physics and collision detection library, written in C++, suitable for games and VR applications. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/jrouwe/JoltPhysics"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
source=("http://github.com/jrouwe/JoltPhysics/archive/refs/tags/v${pkgver}.tar.gz"
        "0001-fix-missing-uint.patch")
options=('staticlibs' '!strip')
sha256sums=('7ebb40bf2dddbcf0515984582aaa197ddd06e97581fd55b98cb64f91b243b8a6'
            'b1f178d1cf59fd6f9351756737d815d03ac910a2e2278060dde5935d76f11a77')

prepare() {
  cd "${srcdir}/JoltPhysics-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-fix-missing-uint.patch"
}


build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  
  declare -a debug_symbols
  if check_option "debug" "n"; then
    debug_symbols+=("-DGENERATE_DEBUG_SYMBOLS=OFF")
  else
    debug_symbols+=("-DGENERATE_DEBUG_SYMBOLS=ON")
  fi
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      "${debug_symbols[@]}" \
      -DENABLE_ALL_WARNINGS=OFF \
      -DDOUBLE_PRECISION=OFF \
      -DCROSS_PLATFORM_DETERMINISTIC=ON \
      -DUSE_STATIC_MSVC_RUNTIME_LIBRARY=OFF \
      -DINTERPROCEDURAL_OPTIMIZATION=OFF \
      -DOBJECT_LAYER_BITS=16 \
      -DUSE_SSE4_1=OFF \
      -DUSE_SSE4_2=OFF \
      -DUSE_AVX=OFF \
      -DUSE_AVX2=OFF \
      -DUSE_AVX512=OFF \
      -DUSE_LZCNT=OFF \
      -DUSE_TZCNT=OFF \
      -DUSE_F16C=OFF \
      -DUSE_FMADD=OFF \
      -DTARGET_UNIT_TESTS=ON \
      -DTARGET_HELLO_WORLD=ON \
      -DTARGET_PERFORMANCE_TEST=ON \
      -DTARGET_SAMPLES=ON \
      -DTARGET_VIEWER=ON \
      ../JoltPhysics-${pkgver}/Build

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/JoltPhysics-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  
  install -Dm755 "${srcdir}/build-${MSYSTEM}/JoltViewer.exe" "${pkgdir}${MINGW_PREFIX}/bin/JoltViewer.exe"
  install -Dm755 "${srcdir}/build-${MSYSTEM}/Samples.exe" "${pkgdir}${MINGW_PREFIX}/share/doc/jolt-physics/Samples.exe"
  cp -r "${srcdir}/build-${MSYSTEM}/Assets/" "${pkgdir}${MINGW_PREFIX}/share/doc/jolt-physics/Assets/"
}
